package com.lrj.datastream.sink;

import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

/**
 * @author lrj
 * @date 2022/3/29 17:14
 */
public class TestFileSink {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        DataStreamSource<Integer> stream = env.fromElements(1, 2, 3, 4, 5, 6, 7, 8, 9);
        Path path = new Path("output/fileSink");
        StreamingFileSink<Integer> sink = StreamingFileSink
                .<Integer>forRowFormat(path, new SimpleStringEncoder<>("UTF-8"))
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                // 1G归档
                                .withMaxPartSize(1024 * 1024 * 1024)
                                // 归档周期,时间毫秒数
                                .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
                                // 不活跃间隔时间
                                .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
                                .build()
                ).build();
        stream.addSink(sink);
        env.execute(TestFileSink.class.getName());
    }
}
